//actions是需要执行多个 mutations,就是我们一个方法里面，需要触发多个commit，这时候时候我们可以考虑用actions了

import {SET_LOGIN_STATUE, SET_TOKEN, SET_USERINFO} from "@/store/mutations-type";
import {logout} from "@/network/common";
import store from "@/store/index";

export default {
    //关闭登录界面 存储token 存储用户信息
    doLogin(context,payload){
        return new Promise((resolve,reject)=>{
            context.commit(SET_TOKEN,payload.token)//存储token
            context.commit(SET_USERINFO,payload.userInfo)//存储用户信息
            context.commit(SET_LOGIN_STATUE,true)
            resolve()
        })
    },
    //退出登录
    logout(context,payload){
        return new Promise((resolve,reject)=>{
            logout()//退出登录
            localStorage.removeItem("token")//清空token
            context.commit(SET_USERINFO,null)//清空userinfo
            context.commit('initMenus',null)//清空Menus
            context.commit('initPermission',null)//清空Permission
            context.commit(SET_LOGIN_STATUE,false)
            context.commit('clearTab')//清空tab 菜单数据
            context.commit('setEditSelected','dashboard')//设置选中的菜单
            resolve()
        })
    },
    //初始化权限和路由数据
    initRouteAndPermission(context,payload){
        return new Promise((resolv,reject)=>{
            context.commit('initMenus',payload.menuList)//侧边菜单数据存储到store
            context.commit('initPermission',payload.permissionList)//存储权限数据
            context.commit('changeRouteStatue',true)//获取到菜单 标志改变

        })
    }
}